define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'stock/stock/index' + location.search,
                    add_url: 'stock/stock/add',
                    // edit_url: 'stock/stock/edit',
                    // del_url: 'stock/stock/del',
                    multi_url: 'stock/stock/multi',
                    table: 'stock_stock',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                rowStyle:function(row, index){
                    if (row.ystatus == 4) {
                        return{
                            css:{
                                color:'red'
                            }
                        }
                    }else{
                        return{}
                    }
                },
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),class:'stockid'},
                        {field: 'number', title: __('Number')},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},

                        {field: 'explain', title: __('Explain')},
                        {field: 'operator', title: __('Operator')},
                        {field: 'company', title: __('Company')},
                        {field: 'storehouse.name', title: __('Storehouse.name'),searchList: $.getJSON(Config.this_url+"/config/zaxconfig?states=1")},
                        {field: 'status', title: __('状态'),searchList: $.getJSON(Config.this_url+"/config/zaxconfig?states=17")},
                        // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            buttons: [
                                {
                                    name: 'end',
                                    title: __('完结'),
                                    classname: 'btn btn-xs btn-success btn-click end',
                                    icon: 'fa fa-random',
                                    hidden:function(row){
                                        if(row.ystatus != 1){
                                            return true;
                                        }
                                    },
                                    click: function(data){
                                        var report_id =$(this).parents('tr').find('.stockid').html()
                                        layer.confirm('是否完结库存盘货？', {
                                            btn: ['确定','取消'] //按钮
                                        }, function(){
                                            var url = Config.this_url+'/stock/stock/end'
                                            $.post(url,{report_id:report_id},function (res) {
                                                layer.msg(res.msg)
                                                if(res.code == 1){
                                                    layer.closeAll('loading');
                                                    $('.fa-refresh').click();
                                                }else{

                                                }
                                            })
                                        }, function(){

                                        });
                                    }}
                            ],
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            $('table').on('dblclick','td',function(){
                if($(this).parents('tr').find('.btn-editone').length >0){
                    return  false;
                }
                var stockid = $(this).parents('tr').find('.stockid').html()
                var url = Config.this_url+'/stock/stock/detail?ids='+stockid
                // window.open(url);
                layer.open({
                    type: 2, // 1-在content中写html代码， 2-在content中写src路径
                    area: ['80%', '90%'],
                    title: "单据详情",
                    content: url,
                    // content: Config.this_url+'/purchase/requisition',
                })
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            $(".btn-add").click(function () {
                if($('#c-storehouse_id').val()== 0 ){
                    layer.msg('未选中出库仓库')
                    return false;
                }
                layer.open({
                    type: 2, // 1-在content中写html代码， 2-在content中写src路径
                    area: ['70%', '90%'],
                    title: "选择商品",
                    content: 'seach?house_id='+$('#c-storehouse_id').val(),
                })
            })
            Controller.api.bindevent();
        },
        detail:function(){
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'stock/stock/detail',
                    table: '',
                    "dragsort_url": "",
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url+ location.search,
                pk: 'id',
                sortName: 'id',
                pageSize:15,
                // searchFormVisible: true,
                searchFormTemplate: 'customformtpl',
                columns: [
                    [
                        // {checkbox: true,operate:false},
                        {field: 'goods_id', title: 'ID', operate: false,class:'goods_id'},
                        {field: 'title', title: __('title'),  operate: false},
                        {field: 'image', title: __('图片'), events: Table.api.events.image, formatter: Table.api.formatter.image,operate: false},
                        {field: 'number', title: __('编号')},
                        {field: 'specs', title: __('规格')},
                        {field: 'stock', title: __('当前库存')},
                        {field: 'old_stock', title: __('盘库时库存')},
                        {field: 'now_stock', title: __('盘库库存'),class:'now_stock'},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            buttons: [
                                {
                                    name: 'returnorder',
                                    title: __('选中'),
                                    classname: 'btn btn-xs btn-success btn-click editstatus',
                                    icon: 'fa fa-fighter-jet',
                                    hidden:function(row){
                                        // if(row.status != '审核'){
                                        //     return true;
                                        // }
                                    },
                                    click: function(data){
                                        var that = $(this);
                                        var goods_id =$(this).parents('tr').find('.goods_id').html()
                                        var report_id = $('.report_id').val()
                                        layer.prompt({
                                            formType: 2,
                                            // value: '0',
                                            title: '请输入数量',
                                            area: ['150px', '50px'] //自定义文本域宽高
                                        }, function(value, index, elem){
                                            if(isNaN(value)){
                                                layer.msg('请填写数字')
                                            }else{
                                                var url = Config.this_url+'/stock/stock/addstock'
                                                $.post(url,{goods_id:goods_id,report_id:report_id,stock:value},function (res) {
                                                    layer.msg(res.msg)
                                                    if(res.code == 1){
                                                        layer.closeAll('loading');
                                                        layer.close(index)
                                                        that.parents('tr').find('.now_stock').html(value)
                                                    }else{

                                                    }
                                                })
                                            }
                                        });
                                    }},

                            ],
                            events: Table.api.events.operate, formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        detail1: function () {

            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});